package com.bw.module_patrol.view

import android.app.TimePickerDialog
import android.graphics.Color
import android.widget.PopupMenu
import android.widget.TimePicker
import android.widget.Toast
import androidx.lifecycle.ViewModelProvider
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.lib_base.path.RouterPath
import com.bw.lib_common.view.BaseMvvmActivity
import com.bw.module_patrol.R
import com.bw.module_patrol.databinding.ActivityEditPatrolBinding
import com.bw.module_patrol.entity.PatrolEntity
import com.bw.module_patrol.viewmodel.PatrolViewModel
import java.text.SimpleDateFormat
import java.util.*

/**
 * 工单-智慧园区-巡逻管理
 */
@Route(path = RouterPath.PATH_EDIT_PATROL_ACTIVITY)
class EditPatrolActivity : BaseMvvmActivity<ActivityEditPatrolBinding>() {

    private val vm by lazy { ViewModelProvider(this)[PatrolViewModel::class.java] }
    @Autowired
    @JvmField
    var patrolId:Int = 0
    override fun observeDataChange() {
        vm.updatePatrolSuccess.observe(this){
            Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show()
            finish()
        }
    }

    override fun initView() {
        ARouter.getInstance().inject(this)
        mDataBinding.back.setOnClickListener { finish() }
        mDataBinding.start.setOnClickListener {
            mDataBinding.start.setTextColor(Color.parseColor("#ffffff"))
            mDataBinding.stop.setTextColor(Color.parseColor("#374151"))
            mDataBinding.start.setBackgroundResource(R.drawable.input_select_box_bg)
            mDataBinding.stop.setBackgroundResource(R.drawable.input_box_bg)
        }

        mDataBinding.stop.setOnClickListener {
            mDataBinding.stop.setTextColor(Color.parseColor("#ffffff"))
            mDataBinding.start.setTextColor(Color.parseColor("#374151"))
            mDataBinding.stop.setBackgroundResource(R.drawable.input_select_box_bg)
            mDataBinding.start.setBackgroundResource(R.drawable.input_box_bg)
        }

        mDataBinding.everyDay.setOnClickListener {
            mDataBinding.everyDay.setTextColor(Color.parseColor("#ffffff"))
            mDataBinding.weekDay.setTextColor(Color.parseColor("#374151"))
            mDataBinding.weekEnd.setTextColor(Color.parseColor("#374151"))
            mDataBinding.everyDay.setBackgroundResource(R.drawable.input_select_box_bg)
            mDataBinding.weekDay.setBackgroundResource(R.drawable.input_box_bg)
            mDataBinding.weekEnd.setBackgroundResource(R.drawable.input_box_bg)
        }

        mDataBinding.weekDay.setOnClickListener {
            mDataBinding.weekDay.setTextColor(Color.parseColor("#ffffff"))
            mDataBinding.everyDay.setTextColor(Color.parseColor("#374151"))
            mDataBinding.weekEnd.setTextColor(Color.parseColor("#374151"))
            mDataBinding.weekDay.setBackgroundResource(R.drawable.input_select_box_bg)
            mDataBinding.everyDay.setBackgroundResource(R.drawable.input_box_bg)
            mDataBinding.weekEnd.setBackgroundResource(R.drawable.input_box_bg)
        }

        mDataBinding.weekEnd.setOnClickListener {
            mDataBinding.weekEnd.setTextColor(Color.parseColor("#ffffff"))
            mDataBinding.everyDay.setTextColor(Color.parseColor("#374151"))
            mDataBinding.weekDay.setTextColor(Color.parseColor("#374151"))
            mDataBinding.weekEnd.setBackgroundResource(R.drawable.input_select_box_bg)
            mDataBinding.everyDay.setBackgroundResource(R.drawable.input_box_bg)
            mDataBinding.weekDay.setBackgroundResource(R.drawable.input_box_bg)
        }

        mDataBinding.startTimeIv.setOnClickListener {
            val calendar = Calendar.getInstance()
            val hour = calendar.get(Calendar.HOUR_OF_DAY)
            val minute = calendar.get(Calendar.MINUTE)

            // 创建 TimePickerDialog
            val timePickerDialog = TimePickerDialog(
                this,
                { _: TimePicker, selectedHour: Int, selectedMinute: Int ->
                    // 处理选择的时间
                    val selectedTime = "$selectedHour:$selectedMinute"
                    mDataBinding.startTimeEt.setText(selectedTime)
                },
                hour,
                minute,
                true // 是否使用 24 小时制
            )
            timePickerDialog.show()
        }

        mDataBinding.endTimeIv.setOnClickListener {
            val calendar = Calendar.getInstance()
            val hour = calendar.get(Calendar.HOUR_OF_DAY)
            val minute = calendar.get(Calendar.MINUTE)

            // 创建 TimePickerDialog
            val timePickerDialog = TimePickerDialog(
                this,
                { _: TimePicker, selectedHour: Int, selectedMinute: Int ->
                    // 处理选择的时间
                    val selectedTime = "$selectedHour:$selectedMinute"
                    mDataBinding.endTimeEt.setText(selectedTime)
                },
                hour,
                minute,
                true // 是否使用 24 小时制
            )
            timePickerDialog.show()
        }

        mDataBinding.peopleIv.setOnClickListener {
            val popupMenu = PopupMenu(this, mDataBinding.peopleIv)

            popupMenu.menuInflater.inflate(R.menu.people_menu,  popupMenu.menu)

            popupMenu.setOnMenuItemClickListener {
                mDataBinding.peopleEt.setText(it.title)
                true
            }

            popupMenu.show()
        }

        mDataBinding.routeIv.setOnClickListener {
            val popupMenu = PopupMenu(this, mDataBinding.peopleIv)

            popupMenu.menuInflater.inflate(R.menu.router_menu,  popupMenu.menu)

            popupMenu.setOnMenuItemClickListener {
                mDataBinding.routeEt.setText(it.title)
                true
            }

            popupMenu.show()
        }

        mDataBinding.addAttendance.setOnClickListener {
            ARouter.getInstance().build(RouterPath.PATH_NFC_ACTIVITY).navigation()
        }

        mDataBinding.save.setOnClickListener {
            vm.updatePatrol(patrolId, PatrolEntity(
                id = patrolId,
                patrolPerson = mDataBinding.peopleEt.text.toString(),
                patrolPersonId = 1,
                patrolPoints = listOf(1,2,3),
                patrolStatus = "1",
                patrolTime = SimpleDateFormat("yyyy-MM-dd").format(Date()),
                taskName = mDataBinding.routeEt.text.toString()
            )
            )
        }
    }

    override fun getLayoutId(): Int = R.layout.activity_edit_patrol

}